package com.settlement.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.settlement.system.common.constant.SystemConstants;
import com.settlement.system.model.entity.SysBaseColumnMatch;
import com.settlement.system.model.entity.SysColumnAppend;
import com.settlement.system.mapper.SysColumnAppendMapper;
import com.settlement.system.model.form.ColumnAppendForm;
import com.settlement.system.model.form.ColumnMatchForm;
import com.settlement.system.service.SysColumnAppendService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * <p>
 * 账单项目字段累加配置 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2023-09-20
 */
@Service
@RequiredArgsConstructor
public class SysColumnAppendServiceImpl extends ServiceImpl<SysColumnAppendMapper, SysColumnAppend> implements SysColumnAppendService {

    private final RedisTemplate redisTemplate;

    @Override
    @SuppressWarnings("unchecked")
    public Long saveForm(ColumnAppendForm columnMatchForm) {
        SysColumnAppend columnAppend = new SysColumnAppend();
        BeanUtil.copyProperties(columnMatchForm, columnAppend);
       this.saveOrUpdate(columnAppend);
       // 删除缓存
        redisTemplate.delete(SystemConstants.BILL_COLUMN_APPEND_PREFIX+columnAppend.getPlatformKey());
       return columnAppend.getId();
    }

    @Override
    @SuppressWarnings("unchecked")
    public boolean deleteMatch(String idsStr) {
        Assert.isTrue(StrUtil.isNotBlank(idsStr), "删除的数据为空");
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<SysColumnAppend> list = this.list(new LambdaQueryWrapper<SysColumnAppend>()
                .eq(SysColumnAppend::getId, ids));
        for (SysColumnAppend columnAppend : list) {
            // 删除缓存
            redisTemplate.delete(SystemConstants.BILL_COLUMN_APPEND_PREFIX+columnAppend.getPlatformKey());
        }
        return this.removeByIds(ids);
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<SysColumnAppend> getByPlatform(String platformKey) {
        // 先从缓存提取数据
        Object value = redisTemplate.opsForValue().get(SystemConstants.BILL_COLUMN_APPEND_PREFIX+platformKey);
        if(value != null){
            return (List<SysColumnAppend>)value;
        }
        List<SysColumnAppend> list = this.list(new LambdaQueryWrapper<SysColumnAppend>()
                .eq(SysColumnAppend::getPlatformKey, platformKey)
                .orderByDesc(SysColumnAppend::getUpdateTime));
        // 加入缓存
        redisTemplate.opsForValue().set(SystemConstants.BILL_COLUMN_APPEND_PREFIX+platformKey, list, 1, TimeUnit.HOURS);
        return list;
    }
}
